package com.infolab.aims.domain.asset;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;


import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.list.LazyList;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.validator.constraints.NotBlank;
import com.infolab.aims.common.SpecialToStringStyle;
import com.infolab.aims.common.bean.AbstractModelBean;
import com.infolab.aims.domain.Department;


@Entity
@Table(name="AMTB_ASSET_MODEL")
public class AssetModel extends AbstractModelBean implements Serializable {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer assetModelId;
	
	@NotBlank(message="{error.empty}")
	private String assetModelCode;
	
	@NotBlank(message="{error.empty}")
	private String assetModelName;
	
	@Column(length=10)
	@NotBlank(message="{error.empty}")
	private String assetType;
	
	@Column(length=50)
	@NotBlank(message="{error.empty}")
	private String assetCategory;
	
	private Integer reorderQuantity;
	
	private Integer maintainOrderQuantity;
	
	private String unitOfMeasurement;
	
	@ManyToOne
	@JoinColumn(name="accessDepartmentId")
	private Department accessDepartment;
	
	@Transient
	private List<AssetEquipment> assetEquipments = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(AssetEquipment.class));
	
	@Transient
	private List<AssetEquipmentBalance> assetEquipmentBalances = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(AssetEquipmentBalance.class));
	
	@Transient
	private List<AssetComponent> assetComponents = LazyList.decorate(new ArrayList(),
		      FactoryUtils.instantiateFactory(AssetComponent.class));
	
	@Column(columnDefinition="number(1,0) default 1 not null")
	private boolean active;
	
	@Transient
	private Float unitPrice;
	
	public AssetModel() {
	
	}
	
	public AssetModel(Integer assetModelId,String assetModelCode,String assetModelName,Float unitPrice) {
		this.assetModelId=assetModelId;
		this.assetModelCode=assetModelCode;
		this.assetModelName=assetModelName;
		this.unitPrice=unitPrice;
	}
	
	public AssetModel(Integer assetModelId,String assetModelCode,String assetModelName) {
		this.assetModelId=assetModelId;
		this.assetModelCode=assetModelCode;
		this.assetModelName=assetModelName;
	}
	
	
	public AssetModel(Integer assetModelId,String assetModelCode,String assetModelName, String assetType) {
		this.assetModelId=assetModelId;
		this.assetModelCode=assetModelCode;
		this.assetModelName=assetModelName;
		this.assetType = assetType;
	}

	public Integer getAssetModelId() {
		return assetModelId;
	}

	public void setAssetModelId(Integer assetModelId) {
		this.assetModelId = assetModelId;
	}

	public String getAssetModelCode() {
		if(assetModelCode!=null)
			return StringUtils.trim(assetModelCode);
		
		return assetModelCode;
	}

	public void setAssetModelCode(String assetModelCode) {
		this.assetModelCode = assetModelCode;
	}

	public String getAssetModelName() {
		return assetModelName;
	}

	public void setAssetModelName(String assetModelName) {
		this.assetModelName = assetModelName;
	}

	public String getAssetType() {
		return assetType;
	}

	public void setAssetType(String assetType) {
		this.assetType = assetType;
	}

	public String getAssetCategory() {
		return assetCategory;
	}

	public void setAssetCategory(String assetCategory) {
		this.assetCategory = assetCategory;
	}
	
	public Integer getReorderQuantity() {
		return reorderQuantity;
	}

	public void setReorderQuantity(Integer reorderQuantity) {
		this.reorderQuantity = reorderQuantity;
	}

	public Integer getMaintainOrderQuantity() {
		return maintainOrderQuantity;
	}

	public void setMaintainOrderQuantity(Integer maintainOrderQuantity) {
		this.maintainOrderQuantity = maintainOrderQuantity;
	}

	public String getUnitOfMeasurement() {
		return unitOfMeasurement;
	}

	public void setUnitOfMeasurement(String unitOfMeasurement) {
		this.unitOfMeasurement = unitOfMeasurement;
	}

	@JsonIgnore
	public Department getAccessDepartment() {
		return accessDepartment;
	}

	public void setAccessDepartment(Department accessDepartment) {
		this.accessDepartment = accessDepartment;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public boolean isEquipment()
	{
		return StringUtils.equalsIgnoreCase(assetType, ASSETTYPE_EQUIPMENT);
	}
	
	public boolean isComponent()
	{
		return StringUtils.equalsIgnoreCase(assetType, ASSETTYPE_COMPONENT);
	}
	
	public List<AssetEquipment> getAssetEquipments() {
		return assetEquipments;
	}

	public void setAssetEquipments(List<AssetEquipment> assetEquipments) {
		this.assetEquipments = assetEquipments;
	}

	public List<AssetComponent> getAssetComponents() {
		return assetComponents;
	}

	public void setAssetComponents(List<AssetComponent> assetComponents) {
		this.assetComponents = assetComponents;
	}

	public Float getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(Float unitPrice) {
		this.unitPrice = unitPrice;
	}
	
	public List<AssetEquipmentBalance> getAssetEquipmentBalances() {
		return assetEquipmentBalances;
	}

	public void setAssetEquipmentBalances(
			List<AssetEquipmentBalance> assetEquipmentBalances) {
		this.assetEquipmentBalances = assetEquipmentBalances;
	}



	public static final String ASSETTYPE_EQUIPMENT="EQUIPMENT";
	public static final String ASSETTYPE_COMPONENT="COMPONENT";

	public static final String ASSETSTATUS_REPAIR="REPAIR";
	public static final String ASSETSTATUS_ACTIVE="ACTIVE";
	public static final String ASSETSTATUS_FAULT="FAULT";
	public static final String ASSETSTATUS_INACTIVE="INACTIVE";

	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(obj instanceof AssetModel)
		{
			AssetModel tmp = (AssetModel)obj;
			
			return StringUtils.equalsIgnoreCase(assetModelCode, tmp.getAssetModelCode());
		}
		else
			return false;
	}
	
	
	@Override
	public int hashCode() {
		if(assetModelCode!=null)
			return super.hashCode();
		else
			return -1;
	}
	
	public String getAuditContentHead()
	{
		Department dept = accessDepartment!=null?accessDepartment:new Department();
		
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
			.append("Asset Model Code",assetModelCode)
			.append("Asset Model Name",assetModelName)
			.append("Asset Type",assetType)
			.append("Asset Category",assetCategory)
			.append("ReOrder Quantity",reorderQuantity)
			.append("MOQ",maintainOrderQuantity)
			.append("Unit",unitOfMeasurement)
			.append("Access Department",(dept.getDepartmentCode()!=null?dept.getDepartmentCode():"All Department")+" "
					+ (dept.getDepartmentName()!=null?dept.getDepartmentName():""))			
			.toString();
	}
	
	public String getAuditContentDetail()
	{
		
		return super.getAuditContentDetail();
	}

	
	
	public String getAuditCode()
	{
		if(StringUtils.isNotEmpty(assetModelCode)){
			return assetModelCode;
		}
		return super.getAuditCode();
	}
	
	
}
